Skip to content

Conversation

@dljsjr
Copy link
Member

@dljsjr dljsjr commented Jul 30, 2025

Type of Change

  • Refactor

Checklist

  • I have performed a self-review of my code
  • I have commented my code in hard-to-understand areas
  • I have verified my changes by testing with a device or have communicated a plan for testing

Depends On

Description of Change

@NoahCornell did some analysis of driver heap usage, and Sonos was
disproportionately above the mean for the heap-space-per-device line fit
on the dataset.

This PR improves memory usage by limiting the information we keep resident
from API responses to the fields that we actually use in regular operation.

Additional background:

One source of heap usage was unbounded memory growth due to task spawning
on a certain error pathway, which he already fixed in a different PR.

Another source was the fact that we were cacheing the entire API
response for the Player and Group information for the LAN's Sonos topology.
These API payloads are pretty large, and have actually gotten larger over time.

This has been the case for this driver for a very long time; the
decision to store the whole response object was made so that we would
have the information available if we needed it in the future for bug
fixes or enhancements.

It turns out that the information we're utilizing hasn't really changed
much over the last few years, so I'm feeling quite comfortable about
excising the majority of the payload information at this point. We see
pretty signifcant memory savings with these changes, and the savings
should scale appreciably with device count, which is a big win.

Summary of Completed Tests

Tested on my personal setup by overriding the driver files directly. Will need to be regression tested by internal QA once this lands on Alpha; the OAuth stuff precludes this from being tested using the PR channel invite.

@github-actions
Copy link

github-actions bot commented Jul 30, 2025

Channel deleted.

@github-actions
Copy link

github-actions bot commented Jul 30, 2025

Test Results

   71 files    455 suites   0s ⏱️
2 355 tests 2 355 ✅ 0 💤 0 ❌
3 978 runs  3 978 ✅ 0 💤 0 ❌

Results for commit a50d490.

♻️ This comment has been updated with latest results.

@github-actions
Copy link

github-actions bot commented Jul 30, 2025

Minimum allowed coverage is 90%

Generated by 🐒 cobertura-action against 8aff210

@dljsjr dljsjr force-pushed the chore/improve-sonos-memory-usage branch 2 times, most recently from fd45493 to 415ad10 Compare October 9, 2025 18:04
@dljsjr dljsjr marked this pull request as ready for review October 9, 2025 18:09
Copy link
Contributor

@NoahCornell NoahCornell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, mostly some comments on being a little more memory efficient but I'm not sure how much they really matter.

Do you have any before and after numbers for how much this will save?

I have also been meaning to redo the analysis I did for 57 on 58 and wonder if we will see any changes from the original bonded set changes since that greatly reduced the amount of memory related crashes. I can try and take a look at that tomorrow along with running the change on my own setup.

return rawget(ret, k)
end

local proxy_newindex = function(_, _, _)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just calling out that this only prevents new keys from being set and will still allow modification of current values. I think this is fine since this is just our driver.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's deliberate.

Comment on lines +118 to +120
for k, v in pairs(SpeakerDiscoveryInfo) do
rawset(ret, k, v)
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if we define the metatable outside of the function (per comment below this) then SpeakerDiscoveryInfo could be the metatable and __index could be updated to handle the methods. I don't think it really matters though and this is fine.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seemed more straight forward to do it this way and still have the lazily evaluated URL keys.

@dljsjr dljsjr force-pushed the chore/improve-sonos-memory-usage branch from 415ad10 to a50d490 Compare October 10, 2025 21:24
@dljsjr dljsjr requested a review from NoahCornell October 10, 2025 21:24
@NoahCornell did some analysis of driver heap usage, and Sonos was
disproportionately above the mean for the heap-space-per-device line fit
on the dataset.

This PR improves memory usage by limiting the information we keep resident
from API responses to the fields that we actually use in regular operation.

Additional background:

One source of heap usage was unbounded memory growth due to task spawning
on a certain error pathway, which he already fixed in a different PR.

Another source was the fact that we were cacheing the entire API
response for the Player and Group information for the LAN's Sonos topology.
These API payloads are pretty large, and have actually gotten larger over time.

This has been the case for this driver for a very long time; the
decision to store the whole response object was made so that we would
have the information available if we needed it in the future for bug
fixes or enhancements.

It turns out that the information we're utilizing hasn't really changed
much over the last few years, so I'm feeling quite comfortable about
excising the majority of the payload information at this point. We see
pretty signifcant memory savings with these changes, and the savings
should scale appreciably with device count, which is a big win.
@dljsjr dljsjr force-pushed the chore/improve-sonos-memory-usage branch from a50d490 to 8aff210 Compare October 13, 2025 17:49
@dljsjr dljsjr merged commit e6f59f7 into main Oct 13, 2025
9 checks passed
@dljsjr dljsjr deleted the chore/improve-sonos-memory-usage branch October 13, 2025 17:49
@github-actions
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants